package cn.dxs.adt.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.wgl.support.adapter.PinnedHeaderAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.dxs.adt.R;
import cn.dxs.adt.bean.AppInfo;
import cn.dxs.adt.utils.ClipboardUtil;
import cn.dxs.adt.utils.Util;
import demo.ViewHolder;

public class AppListPinnedHeaderAdapter extends PinnedHeaderAdapter {

    private Context mContext;
    private ExpandableListView mListView;
    private List<String> parents;
    private Map<String, ArrayList<AppInfo>> datas;

    public AppListPinnedHeaderAdapter(ExpandableListView listView, List<String> parents, Map<String, ArrayList<AppInfo>> datas) {
        this.mContext = listView.getContext();
        this.mListView = listView;
        this.parents = parents;
        this.datas = datas;
    }

    @Override
    public int getHeaderState(int groupPos, int childPos) {
        final int childCount = getChildrenCount(groupPos);
        if (childPos == childCount - 1) {
            return PINNED_HEADER_PUSH_UP;
        } else if (childPos < 0 && !mListView.isGroupExpanded(groupPos)) {
            return PINNED_HEADER_GONE;
        } else {
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureHeader(View headerView, int groupPos, int childPos) {
        if (groupPos > -1) {
            TextView tv = ViewHolder.get(headerView, R.id.tv_parent);
            tv.setText(parents.get(groupPos));
            boolean isExpanded = mListView.isGroupExpanded(groupPos);
            int resId;
            if (isExpanded) {
                resId = R.drawable.qb_down;
            } else {
                resId = R.drawable.qb_right;
            }
            Drawable drawable = ContextCompat.getDrawable(mContext, resId);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tv.setCompoundDrawables(drawable, null, null, null);
        }
    }

    @Override
    public int getGroupCount() {
        return parents == null ? 0 : parents.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        String key = parents.get(groupPosition);
        return datas.get(key).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parents.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String key = parents.get(groupPosition);
        return datas.get(key).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.parent_layout, null);
        }

        TextView tv = ViewHolder.get(convertView, R.id.tv_parent);
        String p = parents.get(groupPosition);
        int count = datas.get(p).size();
        if (count <= 0) {
            tv.setText(parents.get(groupPosition));
        } else {
            tv.setText(parents.get(groupPosition) + "(" + count + ")");
        }
        int resId;
        if (isExpanded) {
            resId = R.drawable.qb_down;
        } else {
            resId = R.drawable.qb_right;
        }
        Drawable drawable = ContextCompat.getDrawable(mContext, resId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tv.setCompoundDrawables(drawable, null, null, null);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_app_info, null);
        }
        Button btn_run = ViewHolder.get(convertView, R.id.btn_run);
        ImageView iv_icon = ViewHolder.get(convertView, R.id.iv_icon);
        TextView tv_name = ViewHolder.get(convertView, R.id.tv_name);
        TextView tv_size = ViewHolder.get(convertView, R.id.tv_size);
        TextView tv_firstInstallTime = ViewHolder.get(convertView, R.id.tv_firstInstallTime);
        TextView tv_lastUpdateTime = ViewHolder.get(convertView, R.id.tv_lastUpdateTime);
        TextView tv_mainEntryClassName = ViewHolder.get(convertView, R.id.tv_mainEntryClassName);
        TextView tv_applicationClassName = ViewHolder.get(convertView, R.id.tv_applicationClassName);
        String key = parents.get(groupPosition);
        AppInfo appInfo = datas.get(key).get(childPosition);
        iv_icon.setImageDrawable(appInfo.appIcon);
        tv_name.setText(appInfo.appName + "(" + appInfo.packageName + ")");
        tv_size.setText(appInfo.apkSize + "  " + appInfo.versionName + "(" + appInfo.versionCode + ")");
        tv_firstInstallTime.setText(appInfo.firstInstallTime);
        tv_lastUpdateTime.setText(appInfo.lastUpdateTime);
        tv_mainEntryClassName.setText(appInfo.mainEntryClassName);
        tv_applicationClassName.setText(appInfo.applicationClassName);

        btn_run.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Util.uninstall(mContext, appInfo.packageName);
                Util.startAPP(mContext, appInfo.packageName);
            }
        });

        btn_run.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Util.gotoAppDetailIntent(mContext, appInfo.packageName);
                return false;
            }
        });

        iv_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                sb.append("#").append(appInfo.appName).append("_").append(appInfo.apkSize).append("_").append(appInfo.versionName).append("_").append(appInfo.versionCode).append("_").append(appInfo.applicationClassName).append("_").append(appInfo.mainEntryClassName).append("\n");
                sb.append("const-string v1, \"").append(appInfo.packageName).append("\"\n");
                sb.append("const-string v2, \"").append(appInfo.sig).append("\"\n");
                sb.append("invoke-interface {v0, v1, v2}, Ljava/util/Map;->put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
                ClipboardUtil.copyContentToClipboard(sb.toString());
                Toast.makeText(mContext, "复制剪贴板成功，去粘贴吧！", Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
